<?php
/**
 * Created by JetBrains PhpStorm.
 * User: vtrinh
 * Date: 10/24/11
 * Time: 3:20 PM
 * To change this template use File | Settings | File Templates.
 */
require 'global.php';

$action = REQUEST('action');
$userId = REQUEST('userId');

$currentGameBlob = getFromPersistence('game_'.$userId);

switch($action){
	case "read":
		echo $currentGameBlob;
		break;
	case "write":
		$gameBlobToSave = stripcslashes(REQUEST('gameBlob'));
			
		$currentGameObject = json_decode($currentGameBlob);
		$gameObjectToSave = json_decode($gameBlobToSave);

		if($gameObjectToSave->saveCounter != ($currentGameObject->saveCounter + 1)){
			qd("** CORRUPT SAVE*** Counter mismatch",$gameObjectToSave,$currentGameObject);
		}
			
		writeToPersistence('game_'.$userId,$gameBlobToSave);
		break;
}